using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

using SqlWebAdmin.CSharp;

public partial class databases : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Roles.IsUserInRole("Database Administrator"))
		{
			btnAddNewDatabase.Visible = true;
		}
		else
		{
			btnAddNewDatabase.Visible = false;
		}
        
        if (!IsPostBack)
        {
           
                lblError.Text = string.Empty;
                lblError.Visible = false;

				Server smoServer = SmoHelper.GetServer();                
                try
                {

                    dgDatabases.DataSource = SmoHelper.ListUserDatabases(smoServer.Databases,false);
                    dgDatabases.DataBind();

                }
                catch (SmoException smoEx)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx));
                    lblError.Visible = true;
                }
                catch (System.Exception ex)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex));
                    lblError.Visible = true;
                }
				finally
				{
					smoServer.ConnectionContext.Disconnect();
				}
            

        }
    }
    
    protected void btnAddNewDatabase_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administration/CreateDatabase.aspx", false);
    }

}
